package com.youme.base

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

/**
 * @Time:2021/4/12 14:48
 * @Author:wujinsheng
 * @Description:
 */
interface SmartRefreshDelegate : OnRefreshLoadMoreListener {
    val mRecyclerView: RecyclerView
    val mSmartRefreshLayout: SmartRefreshLayout
    val mAdapter: RecyclerView.Adapter<*>
    fun loadData()

    fun initRefreshDelegate() {
        mSmartRefreshLayout.apply {
            setOnRefreshLoadMoreListener(this@SmartRefreshDelegate)
            setEnableAutoLoadMore(true)
        }
        mRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
        loadData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        loadData()
        refreshLayout.finishLoadMore()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        loadData()
        refreshLayout.finishRefresh()
    }
}